<template>
  <a-input v-bind="$attrs" :class="prefixCls" :size="size" :value="state">
    <template #addonAfter>
      <img :src="src" @click="handleClick" alt="验证码" />
    </template>
  </a-input>
</template>
<script lang="ts">
import {defineComponent, ref} from 'vue';
import {useDesign} from '/@/hooks/web/useDesign';
import {useRuleFormItem} from '/@/hooks/component/useFormItem';

const props = {
  value: {type: String},
  size: {type: String, validator: (v) => ['default', 'large', 'small'].includes(v)},
  captchaSrc: {type: String, default: ""},
};

export default defineComponent({
  name: 'CaptchaInput',
  inheritAttrs: false,
  props,
  setup(props) {
    const src = ref('');
    src.value = props.captchaSrc
    const handleClick = () => src.value = props.captchaSrc + "?r=" + Math.random();
    const {prefixCls} = useDesign('captcha-input');
    const [state] = useRuleFormItem(props);

    return {prefixCls, state, handleClick, src};
  },
});
</script>
<style lang="less">
@prefix-cls: ~'@{namespace}-captcha-input';

.@{prefix-cls} {
  .ant-input-group-addon {
    padding-right: 0;
    background-color: transparent;
    border: none;

    img {
      min-width: 100px;
      border: #D9D9D9 1px solid;
    }
  }
}
</style>
